<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\RoleModel;
use app\admin\model\MenuModel;
use app\admin\model\MenuRoleModel;

class Role extends Base
{
	 /**
	 * 权限对象
	 * @return mixed
	 */
	public $role_obj;
	/**
	 * 栏目对象
	 * @return mixed
	 */
	public $menu_obj;
	 /**
	 * 构造函数
	 * @return mixed
	 */
	public function __construct()
	{
		parent::__construct();
		$this->role_obj = new RoleModel(); 
		$this->menu_obj = new MenuModel(); 
	}
	 /**
	 * 权限列表
	 * @return mixed
	 */
	public function index()
    {
		$input = input('get.');
		if(isset($input['data'])){
			$data = $this->role_obj->getRoleIndex($input);
			if(!empty($data['list']['data'])){
				foreach($data['list']['data'] as $key=>$val){
					if(is_numeric($val['create_time'])){
						$data['list']['data'][$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
					}
				}
			}
			$result = array('code'=>0,'msg'=>'','count'=>$data['list']['total'],'data'=>$data['list']['data']);
			return json($result);
		}else{
			return view('index');
		}
    }
	 /**
	 * 新增权限页面
	 * @return mixed
	 */
	public function add()
	{
		$menu_list = config('menu_list');
		$menu_strs = '<div class="layui-colla-item">';
		foreach($menu_list as $k=>$v){
			$menu_strs .= '<h2 class="layui-colla-title">'.$v.'</h2>';
			$parent = $this->menu_obj->getAllMenuList(['top_id'=>$k,'parent_id'=>0]);
			$menu_strs .= '<div class="layui-colla-content layui-show">';
				$menu_strs .= '<div class="layui-colla-content layui-show">';
					foreach($parent as $p){
						$menu_strs .= '<div class="layui-colla-item">';
							$menu_strs .= '<h2 class="layui-colla-title">'.$p['name'].'</h2>';
							$menu_strs .= '<div class="layui-colla-content layui-show">';
								$child = $this->menu_obj->getAllMenuList(['top_id'=>$k,'parent_id'=>$p['id']]);
								foreach($child as $c){
									$menu_strs .= '<input type="checkbox" name="menu_ids[]" title="'.$c['name'].'" lay-skin="primary" value="'.$k.'-'.$p['id'].'-'.$c['id'].'" >';
								}
							$menu_strs .= '</div>';
						$menu_strs .= '</div>';
					}
				$menu_strs .= '</div>';
			$menu_strs .= '</div>';
		}
		$menu_strs .= '</div>';
		$this->assign('menu_strs',$menu_strs);
		return view('add');
	}
	/**
	 * 更新权限
	 * @return mixed
	 */
	public function save()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			$flag = $this->role_obj->updateRole($input);
			if($flag){
				$result = [
					'code' => 1,
					'msg'  => '请求成功',
					'list' => $input
				];
			}else{
				$result = [
					'code' => 0,
					'msg'  => '请求失败',
					'list' => $input
				];
			}
			return json($result);
		}else{
			$this->error('更新权限数据，请求不合法！');
		}
	}
	/**
	 * 编辑权限
	 * @return mixed
	 */
	public function edit()
	{
		$input = input('get.');
		$view = RoleModel::get($input['id'])->toArray();
		$MenuRole = new MenuRoleModel();
		$role = $MenuRole->getRoleList(['status'=>0,'role_id'=>$input['id']]);
		$role_id = array();
		if(!empty($role)){
			foreach($role as $r){
				array_push($role_id,$r['module_id']);
			}
		}
		$menu_list = config('menu_list');
		$menu_strs = '<div class="layui-colla-item">';
		foreach($menu_list as $k=>$v){
			$menu_strs .= '<h2 class="layui-colla-title">'.$v.'</h2>';
			$parent = $this->menu_obj->getAllMenuList(['top_id'=>$k,'parent_id'=>0]);
			$menu_strs .= '<div class="layui-colla-content layui-show">';
				$menu_strs .= '<div class="layui-colla-content layui-show">';
					foreach($parent as $p){
						$menu_strs .= '<div class="layui-colla-item">';
							$menu_strs .= '<h2 class="layui-colla-title">'.$p['name'].'</h2>';
							$menu_strs .= '<div class="layui-colla-content layui-show">';
								$child = $this->menu_obj->getAllMenuList(['top_id'=>$k,'parent_id'=>$p['id']]);
								foreach($child as $c){
									if(in_array($c['id'],$role_id)){
										$menu_strs .= '<input type="checkbox" name="menu_ids[]" title="'.$c['name'].'" lay-skin="primary" value="'.$k.'-'.$p['id'].'-'.$c['id'].'" checked >';
									}else{
										$menu_strs .= '<input type="checkbox" name="menu_ids[]" title="'.$c['name'].'" lay-skin="primary" value="'.$k.'-'.$p['id'].'-'.$c['id'].'" >';
									}
								}
							$menu_strs .= '</div>';
						$menu_strs .= '</div>';
					}
				$menu_strs .= '</div>';
			$menu_strs .= '</div>';
		}
		$menu_strs .= '</div>';
		$this->assign('view', $view);
		$this->assign('menu_strs',$menu_strs);
		return view('edit');
	}
	/**
	 * 删除权限
	 * @return mixed
	 */
	public function del()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			$flag = $this->role_obj->delRole($input);
			if($flag){
				$result = [
					'code' => 1,
					'msg'  => '请求成功',
					'list' => $input
				];
			}else{
				$result = [
					'code' => 0,
					'msg'  => '请求失败',
					'list' => $input
				];
			}
			return json($result);
		}else{
			$this->error('更新权限数据，请求不合法！');
		}
	}
	
	/**
	 * 更新权限状态
	 * @return mixed
	 */
	public function setStatus()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			$user = RoleModel::get($input['id']);
			$status = 1;
			if($user->status){
				$status = 0;
			}
			$user->status = $status;
			$flag = $user->save();
			if($flag){
				$result = [
					'code' => 1,
					'msg'  => '请求成功',
					'list' => $input
				];
			}else{
				$result = [
					'code' => 0,
					'msg'  => '请求失败',
					'list' => $input
				];
			}
			return json($result);
		}else{
			$this->error('更新权限数据，请求不合法！');
		}
	}
}
